import api from '../../api/api.js'
import {
  JSONdata,
  formatTime,
  heal,
  MarketPrice
} from "../../utils/util.js";
const {
  $Toast
} = require('../../dist/base/index');
Page({
  data: {
     feed: {},
    Page:false,
    carousels: [

    ], // 轮播图
    recentBanner: "",
    acts: [],
    curentItem: {


    },

    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    const a=100;
    for (let i in JSONdata.tabone.forToday[0].items){
   JSONdata.tabone.forToday[0].items[i]["mPrice"] =  Number(Math.floor(Math.random() * 100)) + Number(a)  + Number(JSONdata.tabone.forToday[0].items[i].price);
   }
  
    this.setData({
      carousels: JSONdata.tabone.bannerPics,
      recentBanner: JSONdata.tabone.recentDiscount,
      acts: JSONdata.tabone.forToday
    })
  },
  onReady: function() {
    this.dialog = this.selectComponent("#dialog");
    setInterval(this.interVal, 1000);
  },
  getCell(event) {

    this.setData({
      curentItem: event.currentTarget.dataset.detail
    })
    this.dialog.showDialog();
  },
  setSession(item) {
    var time = formatTime(new Date()).split(" ")[0];
    var that = this;
    item.createTime = time;
    try {
      var value = wx.getStorageSync('items');

      if (value) {
        var arr = new Array();
        for (let i in value) {

          arr.push(value[i].id);

        }
        if (arr.includes(item.id)) {
          $Toast({
            content: '收藏过了',
            type: 'warning'
          });
          return;
        } else {
          value.push(item);
          wx.setStorageSync("items", value);
           
          $Toast({
            content: '收藏成功',
            type: 'success'
          });
        }


      } else {



        var arr = new Array();;
        arr.push(item);
        wx.setStorageSync("items", arr);
        $Toast({
          content: '收藏成功',
          type: 'success'
        });

      }

    } catch (e) {

    }




  },
  lower() {},
  upper() {},
  scroll() {},
  //取消事件 
  _cancelEvent() {

    this.dialog.hideDialog();
  },

  //确认事件 
  _confirmEvent() {
    this.setSession(this.data.curentItem)
    this.dialog.hideDialog();
  },
  handleWarning() {
    $Toast({
      content: '警告的提示',
      type: 'warning'
    });
  },
  interVal() {
    var got = Date.parse(new Date())/1000;
 
    if (got > heal) {
       this.setData({
        Page:false,
        pg: true
      })
    }else{
      this.setData({
        Page: true,
        pg: false
      })
    };
  }
})